//
//  ViewController.m
//  DLNA
//
//  Created by 刘飞 on 2018/12/3.
//  Copyright © 2018 刘飞. All rights reserved.
//

#import "ViewController.h"
#import <MRDLNA.h>
#import <MBProgressHUD.h>
#import "PlayViewController.h"

@interface ViewController ()<UITableViewDelegate, UITableViewDataSource, DLNADelegate>

@property (weak, nonatomic) IBOutlet UITableView *dlnaTable;
@property (nonatomic, strong) NSArray *deviceArr;
@property (nonatomic, strong) MRDLNA *dlnaManager;

@end

@implementation ViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.dlnaManager startSearch];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.dlnaManager = [MRDLNA sharedMRDLNAManager];
    self.dlnaManager.delegate = self;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 发现设备后在代理中可以拿到
- (void)searchDLNAResult:(NSArray *)devicesArray {
    
    NSLog(@"发现设备");
    self.deviceArr = devicesArray;
    [self.dlnaTable reloadData];
}

#pragma mark - UITableViewDelegate & UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.deviceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class]) forIndexPath:indexPath];
    
    CLUPnPDevice *model = self.deviceArr[indexPath.row];
    cell.textLabel.text = model.modelName;
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSString *testUrl = @"http://223.110.239.40:6060/cntvmobile/vod/p_cntvmobile00000000000020150518/m_cntvmobile00000000000659727681";
    if (indexPath.row < self.deviceArr.count) {
        
        CLUPnPDevice *model = self.deviceArr[indexPath.row];
        self.dlnaManager.device = model;
        self.dlnaManager.playUrl = testUrl;
        
        PlayViewController *vc = [[PlayViewController alloc] init];
        vc.model = model;
        [self presentViewController:vc animated:YES completion:nil];
        
    }
}



@end
